package com.vivo.tasks.vivofx;

import com.thinkgem.jeesite.common.persistence.Page;
import com.vivo.common.DatabaseContextHolder;
import com.vivo.finance.entity.ViDeaElectroniccard;
import com.vivo.finance.service.ViDeaElectroniccardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class TaskElectoryCard {
    @Autowired
    private ViDeaElectroniccardService viDeaElectroniccardService;

    public void SyncData() {
        GetElectoryCards("ah");
        GetElectoryCards("js");
    }

    public void GetElectoryCards(String area){
        System.out.println("get electory card from " + area + " db start...");
        Calendar cal = Calendar.getInstance();
        Date startDate = null;
        Date endDate = new Date();

        ViDeaElectroniccard lastItem = new ViDeaElectroniccard();
        lastItem.setTableZone(area);
        lastItem = viDeaElectroniccardService.getLast(lastItem);

        if (lastItem == null) {
            cal.set(2016, Calendar.JULY, 1);
            startDate = cal.getTime();
        } else {
            startDate = lastItem.getFactorytime();
        }

        System.out.println("last change date is :" + startDate);

        int pageStart = 1;
        int pageSize = 5000;

        ViDeaElectroniccard queryOrder = new ViDeaElectroniccard();
        queryOrder.setBeginFactorytime(startDate);
        queryOrder.setEndFactorytime(endDate);

        Page<ViDeaElectroniccard> pageParam = new Page<ViDeaElectroniccard>(pageStart, pageSize);
        pageParam.setOrderBy("a.FACTORYTIME");

        try {
            Helper.changeDB(area);

            Page<ViDeaElectroniccard> pageRes = viDeaElectroniccardService.findPage(pageParam, queryOrder);
            DatabaseContextHolder.setMainDb();

            HandlerCards(pageRes.getList(), area);

            for (int i = pageStart + 1; i <= pageRes.getTotalPage(); i++) {
                System.out.println("electory cards total page is :" + pageRes.getTotalPage() + " cur page is :" + pageRes.getPageNo() + " and pageSize is:" + pageSize);

                pageParam.setPageNo(i);

                Helper.changeDB(area);

                pageRes = viDeaElectroniccardService.findPage(pageParam, queryOrder);
                DatabaseContextHolder.setMainDb();

                HandlerCards(pageRes.getList(), area);
            }
        } finally {
            DatabaseContextHolder.setMainDb();
        }

        System.out.println("get electory card from "+ area + " db end...");
    }

    private void HandlerCards(List<ViDeaElectroniccard> cards, String area) {
        List<ViDeaElectroniccard> orderList = new ArrayList<ViDeaElectroniccard>();

        for (ViDeaElectroniccard card : cards) {
            card.setIsTaskInsert(true);

            if (card.getMainimei() != null && card.getMainimei().length() > 15) {
                card.setMainimei(card.getMainimei().substring(0, 15));
            }

            if (card.getSubimei() != null && card.getSubimei().length() > 15) {
                card.setSubimei(card.getSubimei().substring(0, 15));
            }

            if (card.getRemark() != null && card.getRemark().length() > 8) {
                card.setRemark(card.getRemark().substring(0, 8));
            }

            if (card.getAgentercode() != null && card.getAgentercode().length() > 8) {
                card.setAgentercode(card.getAgentercode().substring(0, 7));
            }

            orderList.add(card);

            if (orderList.size() >= 1000) {
                saveCards(orderList, area);

                orderList.clear();
            }
        }

        if (orderList.size() > 0) {
            saveCards(orderList, area);
        }
    }


    private void saveCards(List<ViDeaElectroniccard> cards, String area) {
        try {
            viDeaElectroniccardService.insertByBatch(cards, area);
        } catch (Exception e) {
            e.printStackTrace();

            for (ViDeaElectroniccard card : cards) {
                card.setTableZone(area);
                try {
                    viDeaElectroniccardService.save(card);
                } catch (Exception ex) {
                    System.out.println("数据已经存在！ err: " + ex.getMessage());

                    // TODO 需要更新数据
                    card.setIsNewRecord(false);
                    card.setIsTaskInsert(false);

                    try {
                        viDeaElectroniccardService.save(card);
                        System.out.println("数据已经成功更新了。");
                    } catch (Exception e1) {
                        System.out.println("数据更新失败" + e1.getMessage());
                    }
                }
            }
        }
    }
}
